package br.com.minhasfinancas.fw.bean;

import br.com.minhasfinancas.fw.interfaces.CrudManager;
import br.com.minhasfinancas.fw.util.CDIUtil;
import br.com.minhasfinancas.fw.util.GenericsReader;

public abstract class AbstractManagedBean<E,ID,M extends CrudManager<E, ID>> extends AbstractBean<E, ID> {
	
	private static final long serialVersionUID = 1L;
	
	private Class<M> crudClass;

	private M crudManager;
	
	@SuppressWarnings("unchecked")
	private Class<M> getCrudClass() {
		if (this.crudClass == null) {
			this.crudClass = (Class<M>) GenericsReader.getTypeArgument(this.getClass(), 2);
		}
		return this.crudClass;
	}

	@Override
	public M getCrudManager() {
		if (this.crudManager == null) {
			this.crudManager = CDIUtil.getSingleton(this.getCrudClass());
		}
		return this.crudManager;
	}

}
